package day11.homework19;

import java.util.InputMismatchException;
import java.util.Objects;
import java.util.Scanner;

/**
 * 学生管理程序的界面
 */
public class StudentUI {
    private SuperArray ary = new SuperArray();
    private Scanner sc = new Scanner(System.in);
    public StudentUI(){
        ary.add(new Student("张飞","男","20"));
        ary.add(new Student("关羽","男","25"));
        ary.add(new Student("刘备","男","30"));
        menu();
    }

    /**
     * 菜单界面
     */
    public void menu(){
        System.out.println("欢迎使用学生管理程序");
        while(true){
            System.out.println("请选择：1-查看所有学生 2-增加 3-修改 4-删除 5-退出 6-搜索");
            int input = 0;
            try {
                input = sc.nextInt();
            }catch (InputMismatchException e){
                System.out.println("输入有误，请重新输入");
                sc.next();
                continue;
            }
            if(input == 1){
                show();
            }else if(input == 2){
                add();
            }else if(input == 3){
                update();
            }else if(input == 4){
                remove();
            }else if(input == 5){
                System.out.println("谢谢使用");
                System.exit(0);
            }else if (input == 6){
                search();
            }
        }
    }

    /**
     * 显示所有学生
     */
    public void show(){
        for(int i = 0 ;i < ary.size();i++){
            Student student = (Student)ary.get(i);
            System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
        }
    }

    /**
     * 增加学生
     */
    public void add(){
        System.out.println("请输入学生姓名");
        String name = sc.next();
        if (name.matches("^[\\u4e00-\\u9fa5]{2,}$") != true){
            System.out.println("输入有误");
            return;
        }
        System.out.println("请输入学生性别");
        String gender = sc.next();
        if (gender.matches("^[男女]$") != true){
            System.out.println("输入有误");
            return;
        }
        System.out.println("请输入学生年龄");
        String age = sc.next();
        if (age.matches("^[1-2][0-9]|[30]$") != true){
            System.out.println("输入有误");
            return;
        }
        ary.add(new Student(name, gender, age));


    }

    /**
     * 修改学生
     */
    public void update(){
        System.out.println("请输入学生姓名");
        String name = sc.next();
        if (name.matches("^[\\u4e00-\\u9fa5]{2,}$") != true){
            System.out.println("输入有误");
            return;
        }
        for (int i = 0;i < ary.size();i++){
            Student student = (Student)ary.get(i);
            if (student.getName().equals(name)){
                System.out.println("请输入要修改的学生信息");
                    System.out.println("请选择：1-修改姓名 2-修改性别 3-修改年龄");
                    int input = sc.nextInt();
                    if(input == 1){
                        System.out.println("请输入新的学生名");
                        String newname = sc.next();
                        if (newname.matches("^[\\u4e00-\\u9fa5]{2,}$") != true){
                            System.out.println("输入有误");
                            return;
                        }
                        student.setName(newname);
                    }else if(input == 2){
                        System.out.println("请输入新的学生性别");
                        String newgender = sc.next();
                        if (newgender.matches("^[男女]$") != true){
                            System.out.println("输入有误");
                            return;
                        }
                        student.setGender(newgender);
                    }else if(input == 3){
                        System.out.println("请输入新的学生年龄");
                        String newage = sc.next();
                        if (newage.matches("^[1-2][0-9]|[30]$") != true){
                            System.out.println("输入有误");
                            return;
                        }
                    }
                    return;
            }
        }
        System.out.println("没有这个学生！");
    }

    /**
     * 删除学生
     */
    public void remove(){
        System.out.println("请输入学生姓名");
        String name = sc.next();
        for (int i = 0;i < ary.size();i++){
            Student student = (Student)ary.get(i);
            if (student.getName().equals(name)){
                ary.remove(i);
                System.out.println("该学生已删除");
                return;
            }
        }
        System.out.println("没有这个学生！");
    }

    /**
     * 搜索学生
     */
    public void search(){
        while (true) {
            System.out.println("请选择搜索学生的方式：1-姓名 2-性别 3-年龄 4-模糊搜索");
            int input = 0;
            try {
                input = sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("输入有误，请重新输入");
                sc.next();
                continue;
            }
            boolean isExist = false;
            if (input == 1) {
                System.out.println("请输入学生姓名：");
                String name = sc.next();
                if (name.matches("^[\\u4e00-\\u9fa5]{2,}$") != true){
                    System.out.println("输入有误");
                    return;
                }
                for (int i = 0; i < ary.size(); i++) {
                    Student student = (Student) ary.get(i);
                    if (student.getName().equals(name)) {
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                        isExist = true;
                    }
                }
                System.out.println("没有这个学生");
            } else if (input == 2) {
                System.out.println("请输入学生性别：");
                String gender = sc.next();
                if (gender.matches("^[男女]$") != true){
                    System.out.println("输入有误");
                    return;
                }
                for (int i = 0; i < ary.size(); i++) {
                    Student student = (Student) ary.get(i);
                    if (student.getGender().equals(gender)) {
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                        return;
                    }
                }
                System.out.println("没有这个性别的学生");
            } else if (input == 3) {
                System.out.println("请输入学生年龄：");
                String inputage = sc.next();
                if (inputage.matches("^[1-2][0-9]|[30]$") != true){
                    System.out.println("输入有误");
                    return;
                }
                for (int i = 0; i < ary.size(); i++) {
                    Student student = (Student) ary.get(i);
                    if (student.getAge() == inputage) {
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                        return;
                    }

                }
                System.out.println("没有这个年龄的学生");
            }else if (input == 4){
                System.out.println("请输入你要搜索的关键字");
                String keyword = sc.next();
                for (int i = 0; i < ary.size(); i++) {
                    Student student = (Student) ary.get(i);
                    if (keyword.matches("^[\\u4e00-\\u9fa5]{2,}$") && ) {
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                        return;
                    }else if (student.getGender() == keyword) {
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                        return;
                    }else if (student.getAge() == keyword) {
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                        return;
                    }

                }

            }
        }
    }

    public static void main(String[] args) {
        new StudentUI();
    }
}
